// pages/task_detl/task_detl.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: "",// 派车单编号
  },
  onMapNav(e) {
    let { lat, lng, name, } = e.currentTarget.dataset
    console.log(lat,lng,name)
    wx.openLocation({
      // latitude: 23.00944,
      // longitude: 113.12249,
      latitude: Number(lat),
      longitude: Number(lng),
      scale: 18,
      name,
      // address: ''
    })
  },
  // 开始、完成任务
  onToggleFlag(e) {
    this.setData({
      flag: !this.data.flag,
    })
  },

  // 233031 确认任务
  onConfirmTask(e) {
    wx.showModal({
      content: '确定确认任务吗？',
      success: (res) => {
        if (res.confirm) { // 发送请求

        }
      },
    })
  },
  // 出车
  onGoOutTask(e) {
    this.onToggleFlag(e)
  },
  // 抵达上车点
  onArriveTripPoint(e) {
    this.onToggleFlag(e)
  },
  // 抵达下车点
  onArriveDestPoint(e) {
    this.onToggleFlag(e)
  },
  // 车场时间
  onArriveParkPoint(e) {
    wx.showModal({
      content: '确定确认收车吗？',
      success: (res) => {
        if (res.confirm) { // 发送请求

        }
      },
    })
  },
  //领取任务
  onReceiveTask() {
    wx.showModal({
      content: '确定领取该任务吗？',
      success: (res) => {
        if (res.confirm) { // 发送请求

        }
      },
    })
  },
  //拒绝任务
  onRefuseTask() {
    wx.showModal({
      content: '确定拒绝该任务吗？',
      success: (res) => {
        if (res.confirm) { // 发送请求

        }
      },
    })
  },
  getDetl() {
    let { orderId } = this.data;
    let postData = {
      "oby": "v_zuchepaichedanmingxibiao.row_id", "table_id": "v_zuchepaichedanmingxibiao", "page": 1, "limit": 10,
      "SearchWhere": { filters: [{ "CompareWith": "v_zuchepaichedanmingxibiao.Order_No", "Operator": "=", "CompareValue": orderId, "or": 0 }], type: 'and' }
    }
    wx.$post(3, 'DynamicQuery/query_dynamic_v3', postData).then(res => {
      if (res.response.errcode != 0) return wx.$toast(res.response.errmsg)
      let { row } = res.data;
      if (row.length == 0) return wx.$toast('获取不到该任务详情')
      row[0].chufa_time = row[0].kaishiriqi ? `${row[0].kaishiriqi ? row[0].kaishiriqi.split(' ')[0] : ''} ${row[0].kaishishijian ? row[0].kaishishijian.split(' ')[1] : ''}` : ''
      row[0].jieshu_time = row[0].jieshuriqi ? `${row[0].jieshuriqi ? row[0].jieshuriqi.split(' ')[0] : ''} ${row[0].jieshushijian ? row[0].jieshushijian.split(' ')[1] : ''}` : ''
      console.log(row[0]);
      this.setData({ detlData: row[0] })
    })
  },
  // 接口
  // getDetl() {
  //   let { oid, did, wid, } = this.data
  //   if (!oid || !did || !wid) return

  //   let pro0 = this.getDispatch(),
  //     pro1 = this.getWayBill()
  //   wx.showLoading()
  //   Promise.all([pro0, pro1]).then(([data0, data1]) => {
  //     wx.hideLoading()
  //     let trips = data0.trip,
  //       details = data1.details

  //     details.forEach((detl, i) => {
  //       detl.pnts = trips[i]
  //       detl.departure_time = detl.departure_time.slice(0, 16)
  //     })

  //     data1.issuer_man = data1.issuer_man || data0.dispatch_staff_name

  //     this.setData({
  //       detlData: data1,
  //       detl2: data0,
  //     })
  //   })
  // },
  getDispatch() {
    let { oid, did, } = this.data,
      params = {
        page: 1,
        count: 1,
        order_id: oid,
        dispatch_id: did,
      }
    return new Promise((resolve, reject) => {
      wx.$get('/dri/sell/order/dispatch/vehicle/list/paging', params).then(res => {
        if (res.code !== 10000) return
        let { rows, } = res.data,
          data = rows[0],
          status = {
            0: { name: '待领取', cls: 'yellow', },
            1: { name: '待执行', cls: 'red', },
            2: { name: '执行中', cls: 'blue', },
            4: { name: '已执行', cls: 'black', },
          },
          statusObj = status[data.dispatch_status_id] || {}

        data.status = statusObj.name || ''
        data.cls = statusObj.cls || ''

        let triparr = [{
          cls: 'start',
          name: data.trip_origin,
          addr: data.trip_origin_address,
          lat: data.trip_origin_latitude,
          lng: data.trip_origin_longitude,
        }]
        if (data.trip_passing_spot_longitude_latitude) {
          let arr = JSON.parse(data.trip_passing_spot_longitude_latitude)
          arr.forEach(item => {
            let { name, longlat, } = item,
              { latitude, longitude, } = longlat
            triparr.push({
              cls: 'via',
              name,
              addr: name,
              lat: latitude,
              lng: longitude,
            })
          })
        }
        triparr.push({
          cls: 'end',
          name: data.trip_destination,
          addr: data.trip_destination_address,
          lat: data.trip_destination_latitude,
          lng: data.trip_destination_longitude,
        })

        let rtnarr = []
        if (data.return_origin) {
          rtnarr.push({
            cls: 'start',
            name: data.return_origin,
            addr: data.return_origin_address,
            lat: data.return_origin_latitude,
            lng: data.return_origin_longitude,
          })
          if (data.return_passing_spot_longitude_latitude) {
            let arr = JSON.parse(data.return_passing_spot_longitude_latitude)
            arr.forEach(item => {
              let { name, longlat, } = item,
                { latitude, longitude, } = longlat
              rtnarr.push({
                cls: 'via',
                name,
                addr: name,
                lat: latitude,
                lng: longitude,
              })
            })
          }
          rtnarr.push({
            cls: 'end',
            name: data.return_destination,
            addr: data.return_destination_address,
            lat: data.return_destination_latitude,
            lng: data.return_destination_longitude,
          })
        }

        data.trip = [triparr, rtnarr]

        resolve(data)
      })
    })
  },
  getWayBill() {
    let { wid, } = this.data,
      params = {
        company_id: 'sys',
        waybill_id: wid,
      }

    return new Promise((resolve, reject) => {
      wx.$get('/dri/waybill/master/slave/get', params).then(res => {
        if (res.code !== 10000) return
        let { data, } = res

        data.issuer_time = data.issuer_time.slice(0, 16)

        resolve(data)
      })
    })
  },
  showImage(e) {
    let { imagesrc } = e.target.dataset;
    if (imagesrc) {
      wx.previewImage({
        urls: [imagesrc],
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let { orderId } = options
    this.setData({ orderId })
    this.getDetl()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
})
